package com.example.wechat.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wechat.model.WechatMessage;
import com.example.wechat.service.WechatMessageService;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageQueryController {

	private final WechatMessageService messageService;

	public MessageQueryController(WechatMessageService messageService) {
		this.messageService = messageService;
	}

	@GetMapping("/api/messages/paged")
	public Page<WechatMessage> getPagedMessages(@RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "20") int size, @RequestParam String from, @RequestParam String to) {
		return messageService.getMessagesWithPagination(page, size, from, to);
	}

	@GetMapping("/api/messages/by-time")
	public List<WechatMessage> getMessagesByTimeRange(@RequestParam String from, @RequestParam String to,
			@RequestParam String startTime, @RequestParam String endTime) {
		return messageService.findMessagesByTimeRange(from, to, startTime, endTime);
	}

	@GetMapping("/api/messages/stats")
	public Map<String, Object> getMessageStats(@RequestParam String user,
			@RequestParam(required = false) String otherUser) {
		return messageService.getMessageStatistics(user, otherUser);
	}

	@GetMapping("/api/messages/unread-count")
	public long getUnreadMessageCount(@RequestParam String user, @RequestParam(required = false) String from) {
		return messageService.countUnreadMessages(user, from);
	}
}